package controllers

import (
	"encoding/json"
	"strings"
	"time"

	"qkleague/errorcode"
	"qkleague/statis"

	log "github.com/cihub/seelog"
)

type StatisController struct {
	LoginedController
}

//获取首页盈利-订单统计数据
// @router /profit-order/headpage [get]
func (p *StatisController) HeadPageProfitOrder() {
	defer p.Output("HeadPageProfitOrder")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-profit-order-headpage.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	data, code := statis.GetQKleagueStatis().QueryUserProfitOrser(p.Userid)
	if code == errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(code)
		p.Resp["data"] = data
	}

	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)
}

//获取用户昨天的营利-订单
// @router /profit-order/yesterday [get]
func (p *StatisController) YesProfitOrder() {
	defer p.Output("YesProfitOrder")

	profit, order, code := statis.GetQKleagueStatis().QueryUserYesProfit(p.Userid)
	if code != errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(code)
	p.Resp["profit"] = profit
	p.Resp["order"] = order
}

//获取用户本月的营利-订单
// @router /profit-order/month [get]
func (p *StatisController) MonthProfitOrder() {
	defer p.Output("MonthProfitOrder")

	profit, order, code := statis.GetQKleagueStatis().QueryUserMonProfit(p.Userid)
	if code != errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(code)
	p.Resp["profit"] = profit
	p.Resp["order"] = order
}

//获取用户所有的营利-订单
// @router /profit-order/all [get]
func (p *StatisController) AllProfitOrder() {
	defer p.Output("AllProfitOrder")

	profit, order, code := statis.GetQKleagueStatis().QueryUserAll(p.Userid)
	if code != errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(code)
	p.Resp["profit"] = profit
	p.Resp["order"] = order
}

//获取用户营利分析数据,洗衣模式分析
// @router /profit-order/analyze [get]
func (p *StatisController) AnalyzeProfitOrder() {
	defer p.Output("AnalyzeProfitOrder")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-profit-order-analyze.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["all_profit"] = tmp["all_profit"]
		p.Resp["all_order"] = tmp["all_order"]
		p.Resp["order_compare"] = tmp["order_compare"]
		p.Resp["profit_compare"] = tmp["profit_compare"]
		return
	}

	profit, order, profitCompare, orderCompare, code := statis.GetQKleagueStatis().AnalyzeProfitOrders(p.Userid)
	if code != errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["all_profit"] = profit
	p.Resp["all_order"] = order
	p.Resp["order_compare"] = orderCompare
	p.Resp["profit_compare"] = profitCompare
}

//获取洗衣房营利-订单数据,洗衣房营收分析
// @router /washroom/analyze [get]
func (p *StatisController) AnalyzeWashroom() {
	defer p.Output("AnalyzeWashroom")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-washroom-analyze.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	data, code := statis.GetQKleagueStatis().AnalyzeWashroomProfitOrders(p.Userid)
	if code != errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["data"] = data
}

//获取用户的月账单
// @router /month/bill [get]
func (p *StatisController) UserMonthBill() {
	defer p.Output("UserMonthBill")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-month-bill.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		p.Resp["bill_msg"] = tmp["bill_msg"]
		return
	}

	billMsg, data, code := statis.GetQKleagueStatis().UserMonthBill(p.Userid)
	if code != errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		return
	} else {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
		p.Resp["bill_msg"] = billMsg
	}
}

//获取用户总营收列表
// @router /user/profit/all/list [get]
func (p *StatisController) QueryUserAllProfitList() {
	defer p.Output("QueryUserAllProfitList")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-user-profit-all-list.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	if all, data, code := statis.GetQKleagueStatis().QueryAllProfit(p.Userid); code == errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		p.Resp["all"] = all
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
	}
}

//获取用月营收列表
// @router /user/profit/month/list [get]
func (p *StatisController) QueryUserMonthProfitList() {
	defer p.Output("QueryUserMonthProfitList")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-user-profit-month-list.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	month := p.GetString("month", "")
	current, _ := p.GetInt("current", 0)

	if all, data, code := statis.GetQKleagueStatis().QuerySpecificMonthProfit(p.Userid, month, current); code == errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		p.Resp["all"] = all
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
	}
}

//获取用户总订单列表
// @router /user/order/all/list [get]
func (p *StatisController) QueryUserAllOrderList() {
	defer p.Output("QueryUserAllOrderList")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-user-order-all-list.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	if all, data, code := statis.GetQKleagueStatis().QueryAllOrders(p.Userid); code == errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		p.Resp["all"] = all
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
	}
}

//获取用月营收列表
// @router /user/order/month/list [get]
func (p *StatisController) QueryUserMonthOrderList() {
	defer p.Output("QueryUserMonthOrderList")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-user-order-month-list.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	month := p.GetString("month", "")
	current, _ := p.GetInt("current", 0)

	if all, data, code := statis.GetQKleagueStatis().QuerySpecificMonthOrders(p.Userid, month, current); code == errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
		p.Resp["all"] = all
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = code
		p.Resp["msg"] = errorcode.Msg(code)
	}
}

//获取用户盈利-订单详情
// @router /user/washer/orders/detail [get]
func (p *StatisController) QueryWashingOrderDetail() {
	defer p.Output("QueryWashingOrderDetail")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/statis-user-washer-order-detail.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		p.Resp["sort_type"] = tmp["sort_type"]
		return
	}

	yesterday, _ := p.GetInt("yesterday", 0)
	day := p.GetString("day", "")
	start, _ := p.GetInt("start", 0)
	length, _ := p.GetInt("length", 0)
	sortType, _ := p.GetInt("sort", 0)

	if length == 0 {
		length = 20
	}
	if sortType == 0 {
		sortType = 1
	}

	if yesterday == 1 {
		tm := time.Now()
		tmp := tm.AddDate(0, 0, -1).Format("2006-01-02 15:04:05")
		day = strings.Split(tmp, " ")[0]
	}

	if data, sort := statis.GetQKleagueStatis().QueryWashingOrderDetail(p.Userid, day, start, length, sortType); data != nil {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["sort_type"] = sort
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

//@router /user/dimension [post]
func (p *StatisController) ObtainQKLUserDimData() {
	defer p.Output("ObtainQKLUserDimData")
	qtype, _ := p.GetInt("qtype", 0)
	checkSum := p.GetString("check_sum", "")

	body := p.Ctx.Input.RequestBody
	log.Infof("receve body, %s", string(body))

	extra := make(map[string]interface{})
	if err := json.Unmarshal(body, &extra); err != nil {
		log.Errorf("StatisController ObtainQKLUserDimData json err. err:%s", err.Error())
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		p.Resp["check_sum"] = checkSum
		return
	}

	err, data := statis.GetQKleagueStatis().GetQKLStatis(qtype, p.Userid, extra)
	if err == nil {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["check_sum"] = checkSum
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_STATIS_PARAMS_INVALID
		p.Resp["msg"] = err.Error()
		p.Resp["check_sum"] = checkSum
	}
}
